﻿using com.nm.library.Excels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using com.nm.library;
using System.Data;
using com.nm;
using com.nm.db.Models.Systems;
using System.ComponentModel.Composition;
using george.Helper;

namespace OrderAPI.ImportExcel
{
    [Export(typeof(ExcelImport))]
    public class OrderStyleDetailExcel : ExcelImport
    {
        /// <summary>
        /// Excel字段映射及校验缓存
        /// </summary>
        /// <returns>字段映射</returns>
        public override Dictionary<string, ImportVerify> DictFields => dictFields;

        private static Dictionary<string, ImportVerify> dictFields = new List<ImportVerify> {
            new ImportVerify{ ColumnName="流水号",FieldName="SerialNo",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,true,true)},
            new ImportVerify{ ColumnName="设计号",FieldName="DesignNo",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,true,true)},
            new ImportVerify{ ColumnName="颜色代码",FieldName="ColorCode",DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,true,true) },
            new ImportVerify{ ColumnName="颜色",FieldName="ColorName",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,true,true)},
            new ImportVerify{ ColumnName="货品简称",FieldName="ShortName",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,true,true)},
            new ImportVerify{ ColumnName="订单性质",FieldName="OrderType",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,true,true)},
            new ImportVerify{ ColumnName="年份",FieldName="YearNo",DataType=typeof(int),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,4,true,true) },
            new ImportVerify{ ColumnName="季节",FieldName="SeasonName",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,true,true)},
            new ImportVerify{ ColumnName="类别",FieldName="Category",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,true,true)},
            new ImportVerify{ ColumnName="系列",FieldName="SerialName",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,true,true)},
            new ImportVerify{ ColumnName="商品层",FieldName="MaterialProperty",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,true,true)},
            new ImportVerify{ ColumnName="款型",FieldName="ModelName",DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,true,true) },
            new ImportVerify{ ColumnName="项目",FieldName="ItemName",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,true,true)},

            new ImportVerify{ ColumnName="领型",FieldName="CollarTypeName",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,false,true)},
            new ImportVerify{ ColumnName="版型",FieldName="ClothesVersionName",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,false,true)},
            new ImportVerify{ ColumnName="订单属性",FieldName="OrderAttribName",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,false,true)},
            new ImportVerify{ ColumnName="供应商代码",FieldName="VendCustCode",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,false,true)},
            new ImportVerify{ ColumnName="供应商名称",FieldName="VendCustName",DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,false,true) },
            new ImportVerify{ ColumnName="设计师",FieldName="DesignName",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,false,true)},

            new ImportVerify{ ColumnName="元素",FieldName="ElementName",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,false,true)},
            new ImportVerify{ ColumnName="廓型",FieldName="CommodityProfileName",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,false,true)},
            new ImportVerify{ ColumnName="厚薄",FieldName="ThicknessName",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,false,true)},
            new ImportVerify{ ColumnName="模特款",FieldName="IsModelStyle",DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,false,true) },
            new ImportVerify{ ColumnName="腰头类型",FieldName="Waistband",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,false,true)},

            new ImportVerify{ ColumnName="成本价",FieldName="CostPrice",DataType=typeof(decimal),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,9,false,true)},
            new ImportVerify{ ColumnName="订倍率",FieldName="OrderRate",DataType=typeof(decimal),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,5,false,true) },
            new ImportVerify{ ColumnName="吊牌价",FieldName="RetailPrice",DataType=typeof(decimal),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,9,false,true)},

            new ImportVerify{ ColumnName="面料类型",FieldName="StuffType",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,false,true)},
            new ImportVerify{ ColumnName="面料",FieldName="StuffName",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,50,false,true)},
            new ImportVerify{ ColumnName="里料",FieldName="Lining",DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,false,true) },
            new ImportVerify{ ColumnName="填充料",FieldName="Filling",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,false,true)},

            new ImportVerify{ ColumnName="上市日期",FieldName="SaleDate",DataType=typeof(DateTime),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,24,false,true)}

        }.ToDictionary(e => e.ColumnName, e => e);

        public override ExcelImportType Type => ExcelImportType.ORDERSTYLEDETAIL;

        public override object SaveImportData(DataTable dt, Dictionary<string, object> extraInfo, int MasterID)
        {
            var index = dt.Rows.Count;
            var msg = $"成功导入{index}条数据";
            try
            {
                OrderAPI.OrderMasterHandler omh = new OrderMasterHandler();
                omh.ImportOrderStyleDetail(dt, MasterID);
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                return new ImportResult()
                {
                    IsSuccess = false,
                    ExtraInfo = ex,
                    Message = ex.Message
                };
            }
            return new ImportResult()
            {
                IsSuccess = true,
                ExtraInfo = "导入成功",
                Message = msg
            };
        }

        public override DataTable GetExportData(string Keys)
        {
            throw new NotImplementedException();
        }

    }
}
